#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/constants.h"
#include "../include/errors.h"
#include "../include/naryTree.h"
#include "../include/header.h"
#include "../include/binsrchtree.h"
#include "../include/general_vfs_op.h"



int create_dir(char parent_path[],char dir_name[])
{
    int res=-1;
    fdesc *fd;

    n_tree* prev_tree;
    if(if_valid_args_(parent_path))
        if(if_valid_args_(dir_name));
        else return 0;
    else return 0;
    if(mainstr==NULL || mainstr->is_mounted!=5)
        return 4;

    if((mainstr->vfs_read->f_usedfDes) == MAXFD)
        return 1;


    if(!validate_name(dir_name))
        return 2;
     remove_last_slash_(parent_path);
    fd=insertIntoFD(parent_path,dir_name,"DIR");
    res=populate(datastr->htable,datastr->ntree,datastr->bst,fd);
  if(res==-4)
        return 3;
    if(res==1)
    {
        //mainstr->vfs_read->f_usedfDes++;
        return 10;
    }

    else return res;

}
int delete_dir(char *P1) //need to remove root
{
    int res=-1;
    fdesc* fd;

    res=if_valid_args_(P1);
    if(!res)
        return 0;

    if(mainstr==NULL || mainstr->is_mounted!=5)
        return 4;
    remove_last_slash_(P1);
    fd=bst_search_fd(datastr->bst,P1);
    if(fd==NULL)
        return 1;
    if(strcmp(fd->f_type,"DIR")!=0)
        return 1;
   
    res=remove_fd_(datastr->htable,datastr->ntree,datastr->bst,fd);
   
    if(res==-6)
        return 2;
    if(res==1)
    {
        mainstr->vfs_read->f_usedfDes--;
        return 10;
    }
    else
        return res;

}

int list_direc(char *P1,int P2,char *P3)
{
    int ret=-1;
    if(if_valid_args_(P1))
        if(if_valid_args_(P3));
        else return 0;
    else return 0;

    if(!(P2==0 || P2==1))
        return  2;

    if(mainstr==NULL || mainstr->is_mounted!=5)
        return 3;
    remove_last_slash_(P1);
    ret=dir_traversal(datastr->ntree,P2,P1,P3);
    if(ret==7 || ret == 17)
        return 1;
    return ret;
}


int move_dir(char *P1,char *P2)
{
    int res=-1;
    char *src_Path,*dest_Path;
    src_Path=(char*)malloc(sizeof(char)*1024);
    dest_Path=(char*)malloc(sizeof(char)*1024);
    strcpy(src_Path,P1);
    strcpy(dest_Path,P2);
    char *src_Dir;
  
    src_Dir=(char*)malloc(sizeof(char)*CHARSIZE);

    fdesc *src_fd,*des_fd,*temp_src_fd;

    if(if_valid_args_(P1))
        if(if_valid_args_(P2));
        else return 0;
    else return 0;

    if(mainstr==NULL || mainstr->is_mounted!=5)
        return 8;
    remove_last_slash_(P1);
  
    src_fd=bst_search_fd(datastr->bst,P1);
  						
    if(src_fd==NULL)
        return 1;
    remove_last_slash_(P2);
    des_fd=bst_search_fd(datastr->bst,P2);
 
    if(des_fd==NULL)
        return 2;
    if(strcmp(src_fd->f_type,"DIR")!=0)
        return 4;
    src_Dir=strrchr(P1,'/');

    if(strlen(P2)>1)
      strcat(P2,src_Dir);
    else
     strcat(P2,src_Dir+1);  
  
    temp_src_fd=bst_search_fd(datastr->bst,P2);
   
    if(temp_src_fd!=NULL)
        return 5;
    if(strcmp(des_fd->f_type,"DIR")!=0)
        return 7;
    if(strncmp( P1, P2, strlen(P1))==0)
        return 6;
    res=move_Function(datastr->ntree,src_Path,dest_Path,"DIR");
   
    if(res==10)
    {
        datastr->bst=NULL;
        datastr->bst=(b_node *)malloc(sizeof(b_node));
        datastr->bst->key=(char*)malloc(sizeof(char)*CHARSIZE);
         
        if(init_bst(datastr->bst,datastr->ntree->data))
        {
            if(bst_recursive_insert(datastr->bst,datastr->ntree->child))
            {
                datastr->htable=NULL;
                datastr->htable=(hashtable *)malloc(sizeof(hashtable));
                initialize_hashtable(datastr->htable);
                if(ht_recursive_insert(datastr->htable,datastr->ntree->child))
                    return 10;
                else
                    return 7;
                return 10;
            }
            else return 7;
        }
        else return 7;
    }
    return res;
}

